import { createApp } from 'vue'
import main from './main.vue'

interface IOptions {
	parentNode?: HTMLElement
}

interface IPositionDialogInterface {
	setDeviceList?: (list: any[]) => void
	setBedList?: (options: Record<string, any>) => void
	setUserInfo?: (info: Record<string, any>, callback: (options: any) => void) => void
	close?: () => void
	$el: string
}

const positionDialogInit = (options: IOptions): IPositionDialogInterface => {
	// 创建元素节点
	const rootNode = document.createElement('div')
	rootNode.id = 'position_dialog'
	// 在body标签内部插入此元素
	document.body.appendChild(rootNode)
	const App = createApp(main)
	return App.mount(rootNode)
}

export const positionDiaLogRemove = () => {
	const rootNode = document.getElementById('#position_dialog')

	rootNode && document.body.removeChild(rootNode)
}

// 返回弹框实例
export const dislogEl: IPositionDialogInterface = positionDialogInit({})

// 设置标签列表
export const setDeviceList = dislogEl.setDeviceList
// 设置用户信息
export const setUserInfo = dislogEl.setUserInfo
// 根据地图获取床位信息
export const setBedList = dislogEl.setBedList
// 清空地图实时信息定时器
export const closePositionDialog = dislogEl.close
